<?php
function exp_reviews_list($nid) {
  global $user;
  $comments = exp_get_reviews_list($nid);
  $items = array();
  $items['comments'] = $comments;
  return theme("exp_reviews_list", array("items" => $items));
}


function exp_reviews_add($nid) {
  exp_get_reviews_list($nid);
  global $user;
  if(!empty($_POST['comment'])) {
    $comment = new stdClass();
    $comment->nid = $nid;
//    $comment->cid = 0;
//    $comment->pid = 0;
    $comment->uid = $user->uid;
    $comment->mail = $user->mail;
    $comment->name = $user->name;
//    $comment->hostname = '127.0.01';
    $comment->created = time();
//    $comment->is_anonymous = 0;
//    $comment->homepage = '';
    $comment->status = COMMENT_PUBLISHED;
    $comment->language = LANGUAGE_NONE;
    $comment->subject = 'Comment';
    $comment->comment_body[$comment->language][0]['value'] = $_POST['comment'];
    $comment->comment_body[$comment->language][0]['format'] = 'filtered_html';
    comment_submit($comment);
    comment_save($comment);
    drupal_goto('experience/'.$nid);
  }else{
    $items = array();
    $node = node_load($nid);
    $items['node'] = $node;
    return theme("exp_reviews_add", array('items' => $items));
  }
}

function profile_reviews_add($uid) {
  global $user;
  $account = user_load($uid);
  $nid = $account->field_cnode[LANGUAGE_NONE][0]['target_id'];
  if(!empty($_POST['comment'])) {
    $comment = new stdClass();
    $comment->nid = $nid;
    $comment->uid = $user->uid;
    $comment->mail = $user->mail;
    $comment->name = $user->name;
    $comment->created = time();
    $comment->status = COMMENT_PUBLISHED;
    $comment->language = LANGUAGE_NONE;
    $comment->subject = 'Comment';
    $comment->comment_body[$comment->language][0]['value'] = $_POST['comment'];
    $comment->comment_body[$comment->language][0]['format'] = 'filtered_html';
    comment_submit($comment);
    comment_save($comment);
    drupal_goto('profile/'.$uid.'/reviews');
  }else{
    $items = array();
    $items['user'] = $account;
    return theme("profile_reviews_add", array('items' => $items));
  }
}

function _get_comments_list($cnid) {
  $query = db_select('comment', 'c');
  $query->fields("c", array('cid', 'nid', 'name', 'uid', ));
}


